package com.taotao.manage.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.bean.EasyUIResult;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.pojo.ItemDesc;
import com.taotao.manage.service.ItemDescService;
import com.taotao.manage.service.ItemService;

@Controller
@RequestMapping("/item")
public class ItemController {

	@Autowired
	ItemService itemService;
	
	@Autowired
	ItemDescService itemDescService;

	private static final Logger LOGGER = LoggerFactory
			.getLogger(ItemController.class);

	/**
	 * 保存商品及商品详情
	 * 
	 * @param item
	 * @param itemDesc
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Void> save(Item item,
			@RequestParam("desc") String desc) {

		// 开始入参执行保存item
		LOGGER.info("开始保存item, 参数为: item={}, desc={}", item, desc);

		try {
			Boolean flag = itemService.saveItemAndItemDesc(item, desc);
			if (flag) {
				// 保存成功
				LOGGER.info("保存item成功, 参数为: item={}, desc={}", item, desc);
				return ResponseEntity.status(HttpStatus.CREATED).build();
			} else {
				// 参数有误返回400
				LOGGER.info("保存item时参数有误, 参数为: item={}, desc={}", item, desc);
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.info("保存item出现异常, 参数为: item={}, desc={}", item, desc);
		}
		// 异常
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	/**
	 * 分页查询item 
	 * 异步 参数: page, rows 
	 * 返回值: ResponseEntity(EasyUIResult)
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryItemByPage(
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "rows", defaultValue = "10") int rows) {
		
		try {
			PageInfo<Item> pageInfo = itemService.queryPageListByWhere(null, page, rows);
			EasyUIResult result = new EasyUIResult();
			result.setTotal(pageInfo.getTotal());
			result.setRows(pageInfo.getList());
			return ResponseEntity.ok(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	/**
	 * 查询商品详情
	 * 异步
	 * 参数: id
	 * 返回值: ItemDesc对象
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/desc/{id}")
	public ResponseEntity<ItemDesc> queryItemDescById(@PathVariable("id") Long id){
		try {
			ItemDesc itemDesc = itemDescService.queryById(id);
			return ResponseEntity.ok(itemDesc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}

	/**
	 * 更新商品及商品详情
	 * @param item
	 * @param itemDesc
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<Void> update(Item item,
			@RequestParam("desc") String desc) {
		try {
			Boolean flag = itemService.updateItemAndItemDesc(item, desc);
			if (flag) {
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			} else {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

}







